package com.tiger.novelbld.base;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tiger.novelbld.R;
import com.wiyun.ad.AdView;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

public class CommonUtil {
	
	public static String filePath = "";
	public static interface sendMessage{
		public static final int READ_TXT=0X01;
	}
	public static interface listnerKeys{
		public static final String read_key="MenuActivityResponse";
	}
	public static void  initAdView(BaseActivity base){
		int b=(int)(Math.random()*2);//产生0-1000的整数随机数
			if(b==0){
	    		CommonUtil.setUMAdView(base, base);
//				CommonUtil.setWYAdView(base);
	    	}else if(b==1){
	    		CommonUtil.setUMAdView(base, base);
//	    		CommonUtil.setWYAdView(base);
	    	}else{
//	    		CommonUtil.setWYAdView(base);
	    		CommonUtil.setUMAdView(base, base);
	    	}
	}
	/**
	 * 置入有米广告
	 * @author:tiger.chenlin (2011-5-11) 
	 * @file:CommonUtil.java 
	 * @return:void
	 * @param act
	 * @param listener
	 */
	public static void setUMAdView(Activity act, net.youmi.android.AdListener listener) {
		// 初始化广告视图
		net.youmi.android.AdView adView = new net.youmi.android.AdView(act, Color.GRAY, Color.WHITE, 50);

		// 设置监听者(可以不设置，对获取广告没有影响)
		adView.setAdListener(listener);

		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
				FrameLayout.LayoutParams.FILL_PARENT,
				FrameLayout.LayoutParams.WRAP_CONTENT);

		// 设置广告出现的位置(悬浮于底部)
		params.bottomMargin = 0;
		params.gravity = Gravity.BOTTOM;
		adView.setBackgroundColor(R.color.clr_blue);
		// 将广告视图加入Activity中
		act.addContentView(adView, params);
	}
	/**
	 * 设置微云广告
	 * @author:tiger.chenlin (2011-5-14) 
	 * @file:CommonUtil.java 
	 * @return:void
	 * @param act
	 * @param listener
	 */
	public static void setWYAdView(Activity act) {
		// 创建广告组件
		AdView ad = new AdView(act);
		// 设置广告位id
		ad.setResId("8ba4e9a9c016cd58");
		
		// 设置为调试模式
		// 不需要调用setTestAdType, 因为缺省情况下是使用横幅广告
//		ad.setTestMode(true);
		
		// 设置无广告时隐藏广告组件
		ad.setGoneIfFail(true);
		FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		lp.gravity = Gravity.BOTTOM;
		lp.bottomMargin= 0;
		act.addContentView(ad, lp);
		// 开始请求广告
		ad.requestAd();
	}

	/**
	 * 得到文本文件的内容，filename包括全路径
	 * @author:tiger.chenlin (2011-5-11) 
	 * @file:CommonUtil.java 
	 * @return:String
	 * @param fileName
	 * @return
	 */
	public static String getTextByCustomPath(InputStream is){
		String text="";
		try{
		BufferedReader br;
		br = new BufferedReader(
				new InputStreamReader(is));
		   StringBuffer expected = new StringBuffer();
		   String s ;
		   while ((s = br.readLine()) != null) {
		    expected.append(s).append("\n");
		   }
		   text=expected.toString();
		}catch(Exception exp){
			exp.printStackTrace();
		}
		return text;
	}
	/**
	 * 得到asset根目录下文本文件的内容
	 * @author:tiger.chenlin (2011-5-11) 
	 * @file:CommonUtil.java 
	 * @return:String
	 * @param context
	 * @param txtNum
	 * @return
	 */
	public static String getTextByTxt(Context context, String fileName) {
		String text = "";
		AssetManager am = context.getAssets();
		InputStream is = null;
		try {
			is = am.open(fileName);
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(is));
			StringBuilder sb = new StringBuilder("");
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			text=sb.toString();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return text;
	}
	
	
}
